home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_147 / sys / atari / atari.zoo / diredsup.c < prev    next >
C/C++ Source or Header  |  1988-07-25  |  7KB  |  314 lines

  1. /* diredsup.c -- Atari ST functions for handling DIR and DIRED features
  2.  *
  3.  * author :  Sandra Loosemore
  4.  * date   :  20 Dec 1987
  5.  *
  6.  */
  7.  
  8. #include "..\..\def.h"
  9.  
  10.  
  11. /* ST-specific code to support the DIR features in dir.c
  12.  */
  13.  
  14. #ifndef NO_DIR
  15.  
  16. char *getwd (buffer)
  17.     char *buffer;
  18. {   int drive, i;
  19.     drive = Dgetdrv();
  20.     buffer[0] = (char)drive + 'a';
  21.     buffer[1] = ':';
  22.     Dgetpath (&buffer[2], drive+1);
  23.     for (i=2; buffer[i] != '\0'; i++)
  24.         buffer[i] = TOLOWER(buffer[i]);
  25.     return(buffer);
  26.     }
  27.  
  28. int chdir (buffer)
  29.     char *buffer;
  30. {   int drive;
  31.     if (buffer[1] == ':')  {
  32.         drive = TOLOWER(buffer[0]) - 'a';
  33.     (VOID) Dsetdrv (drive);
  34.     buffer = buffer + 2;
  35.         }
  36.     return ((int)Dsetpath (buffer));
  37.     }
  38.  
  39. #endif
  40.  
  41.  
  42. /* ST-specific code to support the DIRED features in dired.c.
  43.  */
  44.  
  45. #ifndef NO_DIRED
  46.  
  47. #include "..\..\kbd.h"
  48.  
  49.  
  50. /* Various file manipulation functions.  */
  51.  
  52. int rename (fromname, toname)
  53.     char *fromname, *toname;
  54. {   if (Frename(0, fromname, toname) == 0)
  55.         return(0);
  56.     else  {
  57.         ewprintf ("Rename failed.");
  58.         return(-1);
  59.     }
  60.     }
  61.  
  62. int copy (fromname, toname)
  63.     char *fromname, *toname;
  64. {   int from, to, count;
  65.     char buffer[256];
  66.     if ((from = Fopen (fromname, 0)) < 0)  {
  67.         ewprintf ("Could not open input file %s.", fromname);
  68.         return(-1);
  69.         }
  70.     (VOID) Fdelete (toname);
  71.     if ((to = (Fcreate (toname, 0))) < 0)  {
  72.         ewprintf ("Could not open output file %s.", toname);
  73.         Fclose(from);
  74.     return(-1);
  75.         }
  76.     while ((count = Fread(from, (long)256, buffer)) > 0)
  77.         (VOID) Fwrite (to, (long)count, buffer);
  78.     (VOID) Fclose(from);
  79.     (VOID) Fclose(to);
  80.     return(0);
  81.     }
  82.  
  83. int unlink (fname)
  84.     char *fname;
  85. {   if (Fdelete(fname) == 0)
  86.         return(0);
  87.     else
  88.         return(-1);
  89.     }
  90.  
  91. int unlinkdir (fname)
  92.     char *fname;
  93. {   if (Ddelete(fname) == 0)
  94.         return(0);
  95.     else
  96.         return(-1);
  97.     }
  98.  
  99.  
  100. /* Create a dired buffer for the given directory name.  */
  101.  
  102. BUFFER *dired_(dirname)
  103. char *dirname;
  104. {
  105.     register BUFFER *bp;
  106.     BUFFER *findbuffer();
  107.  
  108.     /* Create the dired buffer */
  109.  
  110.     if ((dirname = adjustname(dirname)) == NULL) {
  111.     ewprintf("Bad directory name");
  112.     return NULL;
  113.         }
  114.     if ((bp = findbuffer(dirname)) == NULL) {
  115.     ewprintf("Could not create buffer");
  116.     return NULL;
  117.         }
  118.     if (bclear(bp) != TRUE) return FALSE;
  119.  
  120.     /* Now fill it in.  */
  121.  
  122.     if (!dirlist (bp, dirname))  {
  123.         ewprintf("Could not read directory");
  124.         return FALSE;
  125.         }
  126.  
  127.     /* Clean up and return */
  128.  
  129.     bp->b_dotp = lforw(bp->b_linep);        /* go to first line */
  130.     (VOID) strncpy(bp->b_fname, dirname, NFILEN);
  131.     if((bp->b_modes[0] = name_mode("dired")) == NULL) {
  132.     bp->b_modes[0] = &map_table[0];
  133.     ewprintf("Could not find mode dired");
  134.     return NULL;
  135.         }
  136.     return bp;
  137.     }
  138.  
  139.  
  140. /* Take the name from the line in the buffer and make a filename out of it.
  141.  */
  142.  
  143. d_makename(lp, fn)
  144. register LINE *lp;
  145. register char *fn;
  146. {
  147.     register char *cp;
  148.     register char ch;
  149.     int i;
  150.  
  151.     if(llength(lp) < 55) return ABORT;
  152.     (VOID) strcpy (fn, curbp->b_fname);
  153.     cp = fn + strlen(fn);
  154.     *cp++ = '\\';
  155.     i = 2;
  156.     while ((ch = lgetc(lp,i)) != ' ')  {
  157.         *cp++ = ch;
  158.     i++;
  159.     }
  160.     *cp = '\0';
  161.     return lgetc(lp, 52) == 'd';
  162. }
  163.  
  164.  
  165. /* Here is all the messy code for getting a directory listing.
  166.  *    It is printed out like
  167.  *
  168.  * 0         1         2         3         4         5
  169.  * 01234567890123456789012345678901234567890123456789012345
  170.  *   name----------><-----size  dd-mmm-yyyy  hh:mm:ss  drw
  171.  *
  172.  */
  173.  
  174. typedef struct dta {
  175.     char junk[21];
  176.     char attrib;
  177.     unsigned int timestamp;
  178.     unsigned int datestamp;
  179.     long filesize;
  180.     char name[14];
  181.     } DTA;
  182.  
  183. static char* months[] = {
  184.     "???", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
  185.     "Aug", "Sep", "Oct", "Nov", "Dec"};
  186.  
  187.  
  188. /* Print a number into the character buffer, bumping up the pointer */
  189.  
  190. static char* printit (str, number, width, fill, zero)
  191.     char* str;
  192.     long number;
  193.     int width;
  194.     char fill;
  195. {   int i;
  196.     if (number == 0)   {
  197.         for (i=1; i<width; i++)
  198.         *str++ = fill;
  199.         if (width == 0)
  200.             ;
  201.         else if (zero) 
  202.         *str++ = '0';
  203.         else
  204.         *str++ = fill;
  205.         return (str);
  206.         }
  207.     else  {
  208.         str = printit (str, (number/10), width-1, fill, FALSE);
  209.         *str++ = (char) ((number%10) + (int) '0');
  210.         return (str);
  211.         }
  212.     }
  213.  
  214.  
  215. /* Print a null-terminated string into the buffer */
  216.  
  217. static char* copyit (to, from, width, fill)
  218.     char *from, *to;
  219.     int width;
  220.     char fill;
  221. {   int i;
  222.     char ch;
  223.     i = 0;
  224.     while (*from != '\0')  {
  225.         ch = *from++;
  226.         *to++ = TOLOWER(ch);
  227.         i++;
  228.     }
  229.     while (i < width)  {
  230.         *to++ = fill;
  231.         i++;
  232.         }
  233.     return (to);
  234.     }
  235.  
  236.  
  237. static char* getdate (datestamp, buffer)
  238.     unsigned int datestamp;
  239.     char* buffer;
  240. {   int date, month, year;
  241.  
  242.     date = datestamp & 31;
  243.     month = (datestamp >> 5) & 15;
  244.     year = (datestamp >> 9) + 1980;
  245.     *buffer++ = ' ';
  246.     *buffer++ = ' ';
  247.     buffer = printit (buffer, (long) date, 2, '0', TRUE);
  248.     *buffer++ = '-';
  249.     buffer = copyit (buffer, months[month], 3, ' ');
  250.     *buffer++ = '-';
  251.     buffer = printit (buffer, (long) year, 4, ' ', TRUE);
  252.     return (buffer);
  253.     }
  254.  
  255.  
  256. static char* gettime (timestamp, buffer)
  257.     unsigned int timestamp;
  258.     char* buffer;
  259. {   int second, minute, hour;
  260.  
  261.     second = (timestamp & 31) * 2;
  262.     minute = (timestamp >> 5) & 63;
  263.     hour = (timestamp >> 11);
  264.     *buffer++ = ' ';
  265.     *buffer++ = ' ';
  266.     buffer = printit (buffer, (long) hour, 2, '0', TRUE);
  267.     *buffer++ = ':';
  268.     buffer = printit (buffer, (long) minute, 2, '0', TRUE);
  269.     *buffer++ = ':';
  270.     buffer = printit (buffer, (long) second, 2, '0', TRUE);
  271.     return (buffer);
  272.     }
  273.  
  274.  
  275. static dirlist (bp, dirname)
  276.     BUFFER *bp;
  277.     char *dirname;
  278. {   
  279.     char fname[NFILEN], buf[NFILEN];
  280.     int status;
  281.     DTA my_dta, *old_dta;
  282.     char *bufptr;
  283.  
  284.     (VOID) strcpy (fname, dirname);
  285.     (VOID) strcat (fname, "\\*.*");
  286.     old_dta = (DTA *) Fgetdta ();
  287.     Fsetdta (&my_dta);
  288.     status = Fsfirst (fname, 0x11);
  289.     while (status == 0)  {
  290.         bufptr = buf;
  291.     *bufptr++ = ' ';
  292.     *bufptr++ = ' ';
  293.         bufptr = copyit (bufptr, my_dta.name, 15, ' ');
  294.         bufptr = printit (bufptr, my_dta.filesize, 10, ' ', TRUE);
  295.         bufptr = getdate (my_dta.datestamp, bufptr);
  296.         bufptr = gettime (my_dta.timestamp, bufptr);
  297.     *bufptr++ = ' ';
  298.     *bufptr++ = ' ';
  299.         *bufptr++ = (my_dta.attrib) ? 'd' : '-';  /* directory */
  300.     *bufptr++ = 'r';
  301.         *bufptr++ = (my_dta.attrib) ? '-' : 'w';  /* read-only */
  302.         *bufptr = '\0';
  303.         if (addline(bp, buf) == FALSE)  {
  304.             Fsetdta (old_dta);
  305.             return (FALSE);
  306.             }
  307.         status = Fsnext ();
  308.         }
  309.     Fsetdta (old_dta);
  310.     return (TRUE);
  311.     }
  312.  
  313. #endif
  314.